package com.peanut.search;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix="peanut.es")
public class ESConfig {
	private List<String> nodes;
	//初始化方法
	@Bean
	public TransportClient initTranSportClient() throws UnknownHostException {
		///setting可以指定集群配置，例如名称 elasticsearch
		TransportClient client=new 
				PreBuiltTransportClient(Settings.EMPTY);
			for (String node: nodes) {
				String host=node.split(":")[0];
				int port=Integer.parseInt(node.split(":")[1]);
				InetSocketTransportAddress address1=
				new InetSocketTransportAddress(
				InetAddress.getByName(host),port);
				//没有提供连接的节点ip:port
				client.addTransportAddress(address1);
			}
		return client;
	}
	public List<String> getNodes() {
		return nodes;
	}

	public void setNodes(List<String> nodes) {
		this.nodes = nodes;
	}
	
}
